<template>
  <div class="dashboard-container">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="params">
      <el-form-item label="联系人：">
        <el-input v-model="params.titleName" placeholder="请输入联系人" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态：" >
        <el-select v-model="params.enabled" placeholder="请选择" clearable>
          <el-option label="禁用" :value="0"></el-option>
          <el-option label="启用" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getData">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleAdd('create')">新增账号</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格数据 -->
    <el-table
      :data="list"
      v-loading="loading"
    >
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="contactPerson" label="联系人"></el-table-column>
      <el-table-column prop="phone" label="联系电话"></el-table-column>
      <el-table-column prop="roleName" label="身份"></el-table-column>
      <el-table-column prop="loginName" label="登陆账号"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="enabled" label="状态">
        <template slot-scope="{row}">
          <el-tag
            sizer="medium"
            :type="row.enabled==0?'warning': 'success'"
            @click="handleEnable(row)"
          >
            {{ row.enabled == 0 ? "禁用" : "启用" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleAdd('update', row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div style="margin-top: 20px;text-align: center;">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="params.pageCount"
        :current-page="params.pageIndex"
        :total="total"
        @current-change="handleSizeChange"
      ></el-pagination>
    </div>

    <!-- 新增/编辑 -->
    <el-dialog
      :title="dialogType=='create'? '新增用户': '编辑用户'"
      :visible.sync='dialogVisible'
      :close-on-click-modal="false"
      @close="dialogVisible=false"
    >
      <el-form :model="temp" label-width="120px">
        <el-form-item label="登陆账号：">
          <el-input v-model="temp.loginName" placeholder="请输入..."/>
        </el-form-item>
        <el-form-item label="登陆密码：">
          <el-input v-model="temp.password" placeholder="请输入..."/>
        </el-form-item>
        <el-form-item label="联系人：">
          <el-input v-model="temp.contactPerson" placeholder="请输入..."/>
        </el-form-item>
        <el-form-item label="联系电话：">
          <el-input v-model="temp.phone" placeholder="请输入..."/>
        </el-form-item>
        <el-form-item label="管理店铺：">
          <el-select v-model="temp.merchantIds" multiple placeholder="请选择店铺" style="width: 100%;">
            <el-option v-for="item in merchantList" :label="item.label" :value="item.value" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { userGetList, userCreate, userUpdate, userEnable } from '@/api/user'
import { merchantGetList } from '@/api/merchant'
import { transfromData } from '@/utils/tools'
import moment from 'moment'
import { ROLE_TYPE } from '@/utils/common'
import md5 from 'js-md5'

export default {
  name: 'User',
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  components: {
  },
  data(){
    return {
      params: {
        titleName: '',  //主体名称关键词
        role: 1,  //用户角色:0-用户超管 1-店铺管理员
        pageIndex: 0,
        pageCount: 10,
        contactPerson: '', //联系人关键词
        enabled: 1
      },
      total: 0,
      loading: false,
      list: [{}],
      dialogVisible: false,
      dialogType: 'create',
      temp:{},
      merchantList: []
    }
  },
  mounted(){
    this.getData()
  },
  methods: {
    async getMerchantList(){
      let data = await merchantGetList({})
      this.merchantList = data.map(item=>{
        return { label: item.name, value: item.id }
      })
    },
    //点击切换分页
    handleSizeChange(val){
      this.params.pageIndex = val - 1;
      this.getData();
    },
    //获取列表数据
    async getData(){
      this.loading = true
      let User = await userGetList(this.params)
      this.loading = false
      this.list = User.list.map(item=>{
        item.createTime = moment(item.createTime).format('YYYY-MM-DD hh:mm:ss')
        item.roleName = transfromData(ROLE_TYPE, item.role)
        return item
      });
      this.total = User.totalCount
    },
    //禁启用
    async handleEnable(row){
      let enabled = row.enabled==1?0:1;
      await userUpdate({ id: row.id, enabled })
      this.$message.success({ message: '操作成功' })
      this.getData();
    },
    //点击新增/编辑
    handleAdd(type, row){
      //获取店铺列表
      this.getMerchantList();
      this.dialogVisible = true;
      this.dialogType = type;
      if(type=='create'){
        this.temp = {
          loginName: '',
          password: '',
          titleName: '',
          contactPerson: '',
          phone: '',
          merchantIds: []
        }
      }else{ 
        let { loginName, titleName, contactPerson, phone, id, merchants } = row;
        this.temp = {
          loginName, titleName, contactPerson, phone, id, 
          merchantIds: merchants?merchants.map(item=>item.id):[]
        }
      }
    },
    //提交表单
    async submit(){
      let { id, loginName, password, contactPerson, phone, merchantIds, titleName } = this.temp;
      if(this.dialogType=='create'){
        let res = await userCreate({
          loginName,  
          contactPerson, 
          phone,
          password: md5(password),
          merchantIds
        })
        //判断需要升级会员
        if(res.code==49){
          this.$refs.vip.visible = true
          this.$message.error({ message: res.message })
          this.dialogVisible = false;
          return;
        }
      }else{
        await userUpdate({
          id, loginName, titleName, 
          contactPerson, phone, merchantIds,
          password: password==undefined?"":md5(password)
        })
      }
      this.$message.success({ message: '操作成功' })
      this.getData();
      this.dialogVisible = false;
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
}
</style>
